import {
	reactive,
	ref
} from "vue";
import utils from '@/utils/utils';
import {
	addUserAddress,
	deleteUserAddress,
	getDefaultAddress,
	getUserAddress
} from '@/config/api/user';
import {
	test
} from "@/uni_modules/uview-plus";
const useAddress = () => {
	const defaultAddress = ref('');
	const addressList = ref([]);
	const address = reactive({
		id: '',
		name: "",
		phone: '',
		province: "",
		city: "",
		area: "",
		addressDetail: "",
		isDefault: false
	});
	//获取用户地址
	const getAddressList = async () => {
		try {
			const res = await getUserAddress();
			addressList.value = res.data;
		} catch (err) {
			utils.showToast('获取地址失败');
		}
	};
	//选择定位地址
	const chooseAddress = () => {
		// #ifndef MP-WEIXIN
		utils.showToast('当前环境不支持选择定位地址');
		// #endif
		// #ifdef MP-WEIXIN
		uni.chooseLocation({
			success: (res) => {
				console.log(res);
			}
		})
		// #endif
	}
	//添加地址
	const addAddress = async () => {
		try {
			if (!address.name) {
				return utils.showToast('请输入收货人姓名');
			}
			if (!address.phone) {
				return utils.showToast('请输入收货人电话');
			}
			if (!test.mobile(address.phone)) {
				return utils.showToast('电话格式不正确');
			}
			if (!address.province) {
				return utils.showToast('请选择省份');
			}
			if (!address.city) {
				return utils.showToast('请选择城市');
			}
			if (!address.area) {
				return utils.showToast('请选择区县');
			}
			if (!address.addressDetail) {
				return utils.showToast('请输入详细地址');
			}
			await utils.showModal("确认提交该地址?");
			const res = await addUserAddress(address);
			if (res.code == 200) {
				utils.showToast('提交成功');
				setTimeout(() => {
					uni.navigateBack();
				}, 700)
			}
		} catch (e) {
			//TODO handle the exception
		}
	}
	//删除地址
	const delAddress = async (id) => {
		try {
			await utils.showModal("确认删除该地址?");
			const res = await deleteUserAddress(id);
			if (res.code == 200) {
				utils.showToast('删除地址成功');
				setTimeout(() => {
					getAddressList();
				}, 500)
			}
		} catch (e) {
			//TODO handle the exception
		}
	}
	//设置默认地址
	const setDefaultAddress = async (data) => {
		try {
			const res = await addUserAddress(data);
			if (res.code == 200) {
				getAddressList();
			}
		} catch (e) {
			//TODO handle the exception
		}
	}
	//获取默认地址
	const getDefault = async () => {
		try {
			const res = await getDefaultAddress();
			if (res.code == 200) {
				defaultAddress.value = res.data;
			}
		} catch (e) {
			//TODO handle the exception
			console.log(e);
		}
	}
	//获取微信地址
	const getWxAddress = () => {
		// #ifndef MP-WEIXIN
		jWeixin.openAddress({
			success: (res) => {
				address.name = res.userName;
				address.phone = res.telNumber;
				address.province = res.provinceName;
				address.city = res.cityName;
				address.area = res.countryName;
				address.addressDetail = res.detailInfo;
				addAddress()
			},
		})
		// #endif
		//#ifdef MP-WEIXIN
		uni.chooseAddress({
			success: (res) => {
				address.name = res.userName;
				address.phone = res.telNumber;
				address.province = res.provinceName;
				address.city = res.cityName;
				address.area = res.countryName;
				address.addressDetail = res.detailInfo;
				addAddress();
			},
			fail: (err) => {}
		})
		//#endif
	};
	return {
		addressList,
		getWxAddress,
		chooseAddress,
		getAddressList,
		addAddress,
		delAddress,
		setDefaultAddress,
		address,
		getDefault,
		defaultAddress
	};
}
export default useAddress;